<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>灾备数据库工具</title>
    <link rel="stylesheet" href="../plugs/layer/theme/default/layer.css">
    <link rel="stylesheet" href="../plugs/layui/css/layui.css">
    <link rel="stylesheet" href="../css/datasyncIndex.css">
    <link rel="stylesheet" href="../css/bootstrap.css">
</head>
<body>
<div class="container-fluid">
    <div class="row">
        <div class="col-lg-12">
            <hr>
        </div>
    </div>
    <div class="row">
        <div class="col-lg-1"></div>
        <div class="col-lg-10">
            <div class="col-lg-6"></div>
            <div class="col-lg-2">
                <label>数据一致性：</label>
                <select name="consistent" id="consistent">
                    <option value="0">一致</option>
                    <option value="1">不一致</option>
                </select>
            </div>
            <div class="col-lg-3">

                <input type="text" name="condition" id="condition" class="form-control">
            </div>
            <div class="col-lg-1">
                <a href="javascript:void(0)" class="btn btn-primary" onclick="seachTable()">搜索</a>
            </div>
        </div>
        <div class="col-lg-1"></div>

    </div>
    <div class="row">
        <div class="col-lg-12">
            <hr>
        </div>
    </div>
    <div class="row">
        <div class="col-lg-1"></div>
        <div class="col-lg-10">
            <table class="table table-bordered">
                    <thead>
                    <tr>
                        <th>序号</th>
                        <th>表名称</th>
                        <th>源表行数</th>
                        <th>目标表行数</th>
                        <th>是否一致</th>
                        <th>操作</th>
                    </tr>
                    </thead>
                    <tbody id="dataTbody">
                    <% var i=0,key,sourceTableRows,targetTableRows;
                    for(var item in tables){
                        key = item["tableName"];
                        sourceTableRows = item["sourceTableRows"];
                        targetTableRows = item["targetTableRows"];
                    if(i%2==0){%>
                    <tr>
                        <td class="tbodyTd">${i+1}</td>
                        <td>${key}</td>
                        <td>${sourceTableRows}</td>
                        <td>${targetTableRows}</td>
                        <td>${sourceTableRows == targetTableRows ? "一致" : "不一致"}</td>
                        <td class="tbodyTd">
                            <button class="btn btn-info" onclick="syncData('${key}')">同步整表数据</button>
                            <button class="btn btn-info" onclick="syncDataNot('${key}')">同步整表数据(手动重建表)</button>
                            <button class="btn btn-info" onclick="diffData('${key}')">数据差分</button>
                        </td>
                    </tr>
                    <%}else{%>
                    <tr class="info">
                        <td class="tbodyTd">${i+1}</td>
                        <td>${key}</td>
                        <td>${sourceTableRows}</td>
                        <td>${targetTableRows}</td>
                        <td>${sourceTableRows == targetTableRows ? "一致" : "不一致"}</td>
                        <td class="tbodyTd">
                            <button class="btn btn-info" onclick="syncData('${key}')">同步整表数据</button>
                            <button class="btn btn-info" onclick="syncDataNot('${key}')">同步整表数据(手动重建表)</button>
                            <button class="btn btn-info" onclick="diffData('${key}')">数据差分</button>
                        </td>
                    </tr>

                    <%}
                    i++;
                    }%>
                    </tbody>
                </table>
        </div>
        <div class="col-lg-1"></div>
    </div>
</div>

<script src="../js/jquery-1.11.1.js" charset="utf-8"></script>
<script src="../plugs/layer/layer.js" charset="utf-8"></script>
<script src="../plugs/layui/layui.js" charset="utf-8"></script>
<script src="../js/bootstrap.js"></script>

</body>
</html>
<script>
    /**
     * 同步整表数据
     * @param tableName
     */
    function syncData(tableName) {
        var conFlag=confirm("该操作执行顺序为：\n1、本地有表--->备份表--->新建表，无表--->新建表。\n2、同步数据到新建表;\n确认要执行该操作吗？");
        if (conFlag){
            var index=layer.load(1,{shade:[0.1,'#cccccc']},'正在同步');
            $.ajax({
                url: '/datasync/doSyncData',
                data: {tableName: tableName},
                dataType: 'json',
                type: 'post',
                success: function (data) {
                    layer.close(index);
                    layer.msg(data.msg);
                }
            });
        }
    }
    /**
     * 同步整表数据不重建表
     * @param tableName
     */
    function syncDataNot(tableName) {
        var conFlag=confirm("该操作执行顺序为：\n1、同步数据到新建表;\n确认要执行该操作吗？");
        if (conFlag){
            var index=layer.load(1,{shade:[0.1,'#cccccc']},'正在同步');
            $.ajax({
                url: '/datasync/doSyncDataNot',
                data: {tableName: tableName},
                dataType: 'json',
                type: 'post',
                success: function (data) {
                    layer.close(index);
                    layer.msg(data.msg);
                }
            });
        }
    }
    /**
     * 追加数据到现有表
     * tableName
     */
    function addtoData(tableName) {
        var conFlag=confirm("该操作执行顺序为：\n1、追加数据到本地数据表，存在同一编号ID数据，检测字段是否变化，有变化更新，无变化不插入。\n确认要执行该操作吗？");
        if (conFlag){
            var index=layer.load(1,{shade:[0.1,'#cccccc']},'正在同步');
            $.ajax({
               url:'/datasync/doAddtoData',
               data:{tableName:tableName},
               dataType:'json',
               type:'post',
               success:function (data) {
                   layer.close(index);
                   if (data.code==0){
                       layer.msg(data.msg);
                   } else{
                       layer.msg(data.msg);
                   }
               }
            });
        }
    }

        /**
     * 追加数据到现有表
     * tableName
     */
    function diffData(tableName) {
        var conFlag=confirm("该操作为对源数据库和目标数据库进行主键对比，将差分的数据写入中间库");
        if (conFlag){
            var index=layer.load(1,{shade:[0.1,'#cccccc']},'正在尽心差分动作？');
            $.ajax({
               url:'/compare/diffData',
               data:{tableName:tableName},
               dataType:'json',
               type:'post',
               success:function (data) {
                   layer.close(index);
                   if (data.code==0){
                       layer.msg(data.msg);
                   } else{
                       layer.msg(data.msg);
                   }
               }
            });
        }
    }

    /**
     * 自定义更新表
     **/
    function customData(tableName) {
        /*
        获取表字段
         */
        /*

         */
    }

    /**
     * 检索表
     */
    function seachTable() {
        var condition=$("#condition").val();

        var consistent=$("#consistent").val();

        $.ajax({
           url:'/datasync/seach',
           data:{"condition":condition,"consistent":consistent},
           dataType:'json',
           type:'post',
           success:function (data) {
                if (data.code==0){
                    $("#dataTbody").empty();
                    var html='';
                    var arr=data.data;
                    console.log(data.data);
                    var j=1;
                    let key,sourceRows,targetRows,isSame;
                    var consistent=data.consistent;
                    for (var i = 0; i < arr.length; i++) {
                        key = arr[i]["tableName"];
                        sourceRows = arr[i]["sourceTableRows"]!=null ? arr[i]["sourceTableRows"] : '';
                        targetRows = arr[i]["targetTableRows"]!=null ? arr[i]["targetTableRows"] : '';
                        if(consistent!=null&&consistent==0){
                          if(sourceRows != targetRows) {
                             continue;
                          }
                        }else if(consistent!=null&&consistent==1){
                          if(sourceRows == targetRows) {
                            continue;
                          }
                        }

                        isSame = sourceRows == targetRows ? "一致" : "不一致";

                        if (i%2==0){
                            html+='<tr>'
                            html+='<td class="tbodyTd">'+j+'</td>'
                            html+='<td>'+key+'</td>'
                            html+='<td>'+sourceRows+'</td>'
                            html+='<td>'+targetRows+'</td>'
                            html+='<td>'+isSame+'</td>'
                            html+='<td class="tbodyTd">'
                            html+='<button class="btn btn-info" onclick="syncData(\''+key+'\')">同步数据</button>'
                            html+='<button class="btn btn-info" onclick="syncDataNot(\''+key+'\')">同步数据(手动重建表)</button>'
                            html+='<button class="btn btn-info" onclick="diffData(\''+key+'\')">数据差分</button>'
                            html+='</td>'
                            html+='</tr>'
                            j++;
                        } else{
                            html+='<tr class="info">'
                            html+='<td class="tbodyTd">'+j+'</td>'
                            html+='<td>'+key+'</td>'
                            html+='<td>'+sourceRows+'</td>'
                            html+='<td>'+targetRows+'</td>'
                            html+='<td>'+isSame+'</td>'
                            html+='<td class="tbodyTd">'
                            html+='<button class="btn btn-info" onclick="syncData(\''+key+'\')">同步数据</button>'
                            html+='<button class="btn btn-info" onclick="syncDataNot(\''+key+'\')">同步数据(手动重建表)</button>'
                            html+='<button class="btn btn-info" onclick="diffData(\''+key+'\')">数据差分</button>'
                            html+='</td>'
                            html+='</tr>'
                            j++;
                        }

                    }
                    $("#dataTbody").append(html);
                }
           }
        });
    }
</script>